package com.diandi.activity.config;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.Map;
import java.util.Properties;

/**
 * Created by 袁恩光 on 2018/1/3.
 */
@Configuration
public class ActivityDbConfig {

    @Bean(name = "activity-dataSource")
    public DataSource getMasterDataSource(Environment environment) throws Exception {
        RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment);
        Map<String, Object> map = propertyResolver.getSubProperties("db.activity.");
        Properties props = new Properties();
        props.put("driverClassName", environment.getProperty("db.driverClassName"));
        props.putAll(map);
        return DruidDataSourceFactory.createDataSource(props);
    }

    /**
     * 提供SqlSeesion
     */
    @Bean(name = "activity-sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("activity-dataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/xmlMapper/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }


    @Bean(name = "activity-sqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("activity-sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "activity-mapperScan")
    public MapperScannerConfigurer mapperScannerConfig() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionTemplateBeanName("activity-sqlSessionTemplate");
        mapperScannerConfigurer.setBasePackage("com.diandi.activity.mapper");
        mapperScannerConfigurer.setAnnotationClass(Repository.class);
        return mapperScannerConfigurer;
    }

    @Bean(name = "message-transactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("activity-dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


}
